#pragma once
#include <stdint.h>
#include "base/core/rect.h"
#include "base/core/point2.h"
#include "Common/SceneDefine.h"
#include "Common/CharacterDefine.h"
#include "Detour/Include/DetourNavMesh.h"
#include "Detour/Include/DetourNavMeshQuery.h"
#include "Common/TableData/MapMapCfg.h"

struct navMeshInfo
{
	dtNavMesh* pdtNavMesh;
	dtNavMeshQuery* pdtNavMeshQuery;
};

class Map 
{
public:
	bool Init(uint32_t Id);
    void UnInit();

public:
	void		SetId(uint32_t m_id);
	void		SetName(const char* name);
	void		SetWidth(uint32_t m_width);
	void		SetHight(uint32_t m_hight);
	uint32_t	GetId() const { return m_pMapCfg->mapId; };
	uint32_t	GetWidth() const { return m_pMapCfg->mapWide; };
	uint32_t	GetHight() const { return m_pMapCfg->mapHeight; };
	uint32_t	GetMapType() const { return m_pMapCfg->mapType; };
	uint32_t    GetMapSubType() const { return m_pMapCfg->mapSubType; }
	bool		IsDynamic() const { return DYNAMIC_MAP == m_pMapCfg->mapType; };
	bool		IsUnion() const { return UNION_HOME_MAP == m_pMapCfg->mapSubType; };

    bool		GetNavPath(const float* srcPos, const float* dstPos, VEC_POINT3& path, VEC_FLAGS& flgs);
	bool		GetIntNavPath(const float* srcPos, const float* dstPos, VEC_PATH& IntPath);

    dtPolyRef GetNearestPoly(float posX, float posY, float posH, float* extents, float* nearPoint);
    bool      FindNearestPos(float posX, float posY, float posH, float* rsPosX, float* rsPosY, float* rsPosH, dtPolyRef* rsPolyRef);
    bool      Raycast(float* startPos, float dir, float length, HitWallParam* hitParam);

    bool      GetHitWallTangentAngle(float hitX, float hitY, float hitH, float dir, float* rsTangentDir);
    bool      GetPolyHeight(dtPolyRef polyRef, float posX, float posY, float* height);
    bool      CheckPolyFlag(dtPolyRef polyRef, SamplePolyFlags flag);

    dtNavMesh* GetNavMesh() {return m_navMesh;}

	void	  GetPolyFlags(dtPolyRef ref, uint_least16_t* flags);

	bool	  IsInSafeArea(Point3<float> & nPos);
	uint32_t  GetOpenLv(){ return m_pMapCfg->openLv; };
protected:
	bool  _LoadConfig(uint32_t m_id);
	bool  _LoadNavMesh(const char* name);
    dtPolyRef _GetNearestPoly2D(float posX, float posY, float* extents, float* nearestPt);

protected:
    dtNavMesh*      m_navMesh;
    dtNavMeshQuery* m_navMeshQuery;
    dtPolyRef       m_pathPolys[MAX_NAV_POLYS];
    float           m_straightPathPoints[MAX_NAV_POINT_VALUE];
    char            m_name[MAP_NAME_MAX_LENGHT];
    dtQueryFilter   m_normalFilter;
	const MapMapCfgInfo*  m_pMapCfg;

public:
    Map();
    ~Map();
};

//#include "Map.inl"